<?php
/**
 * 核心Db框架
 * @author sunhaoqiang(shqdev@163.com)
 * @time: 2019-08-25 16:38
*/
namespace app\controller;

use app\item\Paper as PaperItem;
use app\model\Paper as PaperModel;
use app\model\Base as BaseModel;


class Paper
{
    public function execute()
    {
        echo '<pre>';
        $this->treeDir();
        echo 'success';
    }

    public function treeDir($dir='./public/*')
    {
        $files = glob($dir);
        foreach ($files as $value) {
            if(is_dir($value)){
                $this->treeDir($value.'/*');
            }
            if(is_file($value)){
                if (pathinfo($value, PATHINFO_EXTENSION) == 'txt') {
                    // echo $value."\n";
                    $this->load($value);
                }
            }
        }
    }

    public function load($file = './public/paper-2-2-534.txt')
    {
        // var_dump($file);die;
        $data = file_get_contents($file);
        $res = json_decode($data);

        $paper = [];
        $tizu = [];
        $questions = [];

        $paperItem = new PaperItem();
        // 循环试卷列表
        foreach ($res as $key => $value) {
            // echo '<pre>';
            if (isset($value->_meta)) {
                $paper_data = $paperItem->paperSingle((array)$value->_meta);
                // 封装试卷数据
                if (!empty($paper_data)) {
                    
                    array_push($paper, $paper_data);
                    
                    // 循环题组列表
                    foreach ($value->content as $k => $v) {
                        // 封装题组数据
                        $v->paper_id = $value->_meta->pid;
                        $tizu_item = $paperItem->tizuSingle($v);

                        // var_dump($tizu_item);die;
                        array_push($tizu, $tizu_item);

                        if (isset($v->scores)) {
                            $scores = [];
                            foreach ($v->scores as $k_s => $v_s) {
                                $scores[$k_s] = (array)$v_s;
                            }
                        }
                        
                        foreach ($v->questions as $k_q => $v_q) {
                            // $v_q->is_child = null;
                            // 二级题目处理
                            // tip: 分数
                            if (!empty($v_q->list)) {
                                $list = (array)$v_q->list;
                                $list = array_map([$paperItem ,'questionSingle'], $list);
                                $questions = array_merge($questions, $list);
                                $v_q->list = implode(',', array_column($list, 'question_id'));
                            }

                            if (isset($scores)) {
                                $v_q->score = $scores[$v_q->question_id]['score'];
                                $v_q->subScore = is_array($scores[$v_q->question_id]['subScore'])? json_encode($scores[$v_q->question_id]['subScore']): $scores[$v_q->question_id]['subScore'];
                            }
                            $p_list = $paperItem->questionSingle($v_q);
                            array_push($questions, $p_list);
                        }
                    }
                }
            }else {
                echo "files-shq: ".$file."\n";
                var_dump($value);
            }            
        }

        $this->paperModel = new BaseModel();
        // $this->insertPaper($paper, 'p_paper');
        // $this->insertPaper($tizu, 'p_tizu');
        $this->insertPaper($paper, 'p_paper');
        $this->insertPaper($tizu, 'p_tizu');
        $this->insertPaper($questions, 'p_question');

        


        // $paperModel->setTable('p_tizu');
        // $paperModel->insertAll($tizu);

        // $paperModel->setTable('p_question');
        // $paperModel->insertAll($questions);
    }
    public function insertPaper($paper, $table)
    {
        if (!empty($paper)) {
            $res = $this->paperModel->setTable($table)->insertAll($paper);
            // $res = $this->paperModel->insertAll($paper);
            if ($res !== true) {
                var_dump($res);
            }
        }
    }
    
}